package sg.edu.nus.iss.billing.template;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import sg.edu.nus.iss.billing.exception.TemplateEngineException;

/**
 * @author Daniel
 *
 */
public class BillingTemplate {
	
	private String templateFileName;
	private String templateFileContent;
	
	public BillingTemplate(String templateFileName) throws TemplateEngineException {
		this.templateFileName = templateFileName;
		loadFile(templateFileName);
	}
	
	private void loadFile(String fileName) throws TemplateEngineException {
		File f = new File(templateFileName);
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(f);
			BufferedReader bf = new BufferedReader(new InputStreamReader(fs));
			String str = "";
			StringBuffer sb = new StringBuffer();
			while ((str=bf.readLine()) != null) {
				sb.append(str);
			}
			this.templateFileContent = sb.toString();
		} catch (FileNotFoundException e) {
			throw new TemplateEngineException(e.getMessage());
		} catch (IOException e) {
			throw new TemplateEngineException(e.getMessage());
		}
	} 
	
	public String getTemplateFileName() {
		return templateFileName;
	}
	public String getTemplateFileContent() {
		return this.templateFileContent;
	}
}
